<?php
include_once('../lib/config.php');
include_once('../lib/Db.class.php');
include_once('../lib/Model.class.php');
include_once('../lib/Snoopy.class.php');
include_once('../lib/parseHtml.class.php');
include_once('../lib/function.php');
include_once('/usr/local/xunsearch/sdk/php/lib/XS.php');

// 定期抓取有更新的书籍
$redis = new Redis();  
$ret = $redis->connect("localhost","6379");  //php客户端设置的ip及端口
if(!$ret) die('redis connction faild');
$redis->auth(REDIS_PASS);
$redis->select(2);

$parseHtml = new parseHtml;
$listurls = [
    'http://www.biquge.com.tw/xuanhuan/',
    'http://www.biquge.com.tw/xiuzhen/',
    'http://www.biquge.com.tw/dushi/',
    'http://www.biquge.com.tw/lishi/',
    'http://www.biquge.com.tw/wangyou/',
    'http://www.biquge.com.tw/kehuan/',
    'http://www.biquge.com.tw/kongbu/',
    'http://www.biquge.com.tw/quanben/',
];

for ($i=1; $i <= 576; $i++) { 
    $listurls[] = sprintf('http://www.vodtw.com/book/ShowBookList.aspx?page=%d', $i);
}

$snoopy = new Snoopy;
foreach ($listurls as $kk=>$urlitem) {
    $retry = 0;
    while (++$retry) {      
        $snoopy->fetch($urlitem);
        echo2("retry:{$retry}, Status:".$snoopy->status.", url:{$urlitem}");

        $parseResult = $parseHtml->getListBook($urlitem, $snoopy->results, $snoopy);
        if(!$parseResult) {
            print_r($snoopy->cookies);
            echo2('该url地址没有相应解析');
            sleep(1);
            continue;
        }

        foreach ($parseResult as $_data) {
            echo2($kk.', '.$_data['title'].', '.$_data['url']);
            $redis->hSet('book_ongoing2', $_data['md5id'], json_encode($_data));
        }

        break;
    }

    if($kk >= 10) {
        break;
    }
}

$it = NULL;
$redis->setOption(Redis::OPT_SCAN, Redis::SCAN_RETRY);
while($arr_keys = $redis->hScan('book_ongoing', $it)) {
    foreach($arr_keys as $str_field => $str_value) {
        $redis->rPush('BOOK_CHAPTER_QUEUE', $str_value);
        echo2($str_field);
    }
}
/*
$it = NULL;
$redis->setOption(Redis::OPT_SCAN, Redis::SCAN_RETRY);
while($arr_keys = $redis->hScan('book_ongoing2', $it)) {
    foreach($arr_keys as $str_field => $str_value) {
        $redis->rPush('BOOK_CHAPTER_QUEUE2', $str_value);
        echo2($str_field);
    }
}
*/
